package io.github.wanggit.antrpc.console.zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class DefaultZkClient implements IZkClient {

    @Value("${antrpc.zookeeper}")
    private String hosts;

    private CuratorFramework curatorFramework;

    @PostConstruct
    public void initCuratorFramework() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5, 30000);
        curatorFramework =
                CuratorFrameworkFactory.builder()
                        .connectString(hosts)
                        .connectionTimeoutMs(5000)
                        .retryPolicy(retryPolicy)
                        .build();
        curatorFramework.start();
    }

    @Override
    public CuratorFramework getCuratorFramework() {
        return curatorFramework;
    }
}
